const Constant = require('./constants');

const http = require('./http.js');

const storage = require('./storage.js');

/**
 * 是否授权小程序登录
 */
function getIsSuccess() {
    let success = uni.getStorageSync(Constant.IS_SUCCESS_KEY);
	if(!success){
		return false;
	}
    return true;
}

/**
 * 小程序登录信息
 */
function getMiniUser() {
    let user = uni.getStorageSync(Constant.APP_USER_INFO);
	if(null == user || "" == user || undefined == user){
		return false;
	}
    return user;
}

/**
 * 平台用户登录信息
 */
function getUser() {
    let user = uni.getStorageSync(Constant.GUO_USER_KEY);
	if(null == user || "" == user || undefined == user){
		return false;
	}
    return user;
}

/**
 * 平台用户token
 */
function getToken() {
	let user = getUser();
    return user ? uni.getStorageSync(Constant.USER_TOKEN_KEY) : '';
}

/**
 * 平台用户id
 */
function getUserId() {
    let user = getUser();
    return user ? user.userId : '';
} 

/**
 * 退出登录
 */
function clearCache() {
	uni.removeStorageSync(Constant.GUO_USER_KEY);
	uni.removeStorageSync(Constant.GUO_IS_LOGIN);
	uni.removeStorageSync(Constant.USER_TOKEN_KEY);
	uni.removeStorageSync(Constant.BIND_USER);
	uni.removeStorageSync(Constant.APP_USER_INFO);
} 

/**
 * 是否登录
 */
function isLogin() {
	let login = uni.getStorageSync(Constant.GUO_IS_LOGIN);
	if(login){
		return true;
	}
	return false;
} 

function logout(){
	//请求后台退出接口
	var params = {
		baseUrl:true,
	    url: '/p/user/logout',
	    method: 'GET',
	    data: { },
	    callBack: function (res) {
	        if(res){
				clearCache();
			}
	    }
	};
	http.request(params);
}



module.exports = {
    
    logout: logout,
	clearCache:clearCache,
    //获取TOKEN
    getToken: getToken,
    //是否已登录
    isLogin: isLogin,
    getUserId: getUserId,
    getUser: getUser,
	getMiniUser:getMiniUser,
	getIsSuccess:getIsSuccess
    
};
